<?php
/*
* Description: Create all custom fields in custom post type  property.
* Version: 1.0
* Author: huuducvt
* Author URI: http://zonthemes.com/author/huuducvt
*/

add_action('admin_menu' ,'zontheme_property_metaboxes');
add_action('save_post', 'zontheme_property_metaboxes_save');


function zontheme_property_metaboxes() {
	add_meta_box('zonthemes_property_metabox', 'Property Information', 'zontheme_property_metaboxes_view', 'property', 'advanced', 'high' );
}

function zontheme_property_metaboxes_view() {
	global $post;
	include(INCLUDES_PATH.'/posttypes/property/zonthemes-property-metaboxes-view.php');
}

function zontheme_property_metaboxes_save($post_id) {
	global $post;
	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
	if(isset($_POST['post_type'])) {
		if('page' == $_POST['post_type']) {
			if(!current_user_can('edit_page', $post_id)) { return $post_id; }
			if(!current_user_can('edit_post', $post_id)) { return $post_id; }
		}
	}
	if(isset($_POST['zon_area'])) { update_post_meta($post_id, 'zon_area', htmlspecialchars($_POST['zon_area'])); } 
	if(isset($_POST['zon_bedroom'])) { update_post_meta($post_id, 'zon_bedroom', htmlspecialchars($_POST['zon_bedroom'])); } 
	if(isset($_POST['zon_tolet'])) { update_post_meta($post_id, 'zon_tolet', htmlspecialchars($_POST['zon_tolet'])); } 
	if(isset($_POST['zon_lease'])) { update_post_meta($post_id, 'zon_lease', htmlspecialchars($_POST['zon_lease'])); }
	if(isset($_POST['zon_garage'])) { update_post_meta($post_id, 'zon_garage', htmlspecialchars($_POST['zon_garage'])); } 
	if(isset($_POST['zon_price'])) { update_post_meta($post_id, 'zon_price', htmlspecialchars(zonthemes_convert_save($_POST['zon_price']))); } 
	if(isset($_POST['zon_address'])) { update_post_meta($post_id, 'zon_address', htmlspecialchars($_POST['zon_address'])); } 
	if(isset($_POST['zon_latitude'])) { update_post_meta($post_id, 'zon_latitude', htmlspecialchars($_POST['zon_latitude'])); }
	if(isset($_POST['zon_longitude'])) { update_post_meta($post_id, 'zon_longitude', htmlspecialchars($_POST['zon_longitude'])); }
	if(isset($_POST['agents'])) { update_post_meta($post_id, 'agents', $_POST['agents']); }
}

/*featured metabox*/
add_action('admin_menu' ,'zontheme_property_feature_metaboxes');
add_action('save_post', 'zontheme_property_feature_metaboxes_save');


function zontheme_property_feature_metaboxes() {
	add_meta_box('zonthemes_property_feature_metabox', 'Featured', 'zontheme_property_feature_metaboxes_view', 'property', 'side', 'high' );
}

function zontheme_property_feature_metaboxes_view() {
	global $post;
	include(INCLUDES_PATH.'/posttypes/property/zonthemes-property-featured-view.php');
}

function zontheme_property_feature_metaboxes_save($post_id) {
	global $post;
	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
	if(isset($_POST['post_type'])) {
		if('page' == $_POST['post_type']) {
			if(!current_user_can('edit_page', $post_id)) { return $post_id; }
			if(!current_user_can('edit_post', $post_id)) { return $post_id; }
		}
	}
	update_post_meta($post->ID, "zon_featured", $_POST["zon_featured"]);
	
}

/*status metabox*/
add_action('admin_menu' ,'zontheme_property_status_metaboxes');
add_action('save_post', 'zontheme_property_status_metaboxes_save');


function zontheme_property_status_metaboxes() {
	add_meta_box('zonthemes_property_status_metabox', 'Status', 'zontheme_property_status_metaboxes_view', 'property', 'side', 'high' );
}

function zontheme_property_status_metaboxes_view() {
	global $post;
	include(INCLUDES_PATH.'/posttypes/property/zonthemes-property-status-view.php');
}

function zontheme_property_status_metaboxes_save($post_id) {
	global $post;
	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
	if(isset($_POST['post_type'])) {
		if('page' == $_POST['post_type']) {
			if(!current_user_can('edit_page', $post_id)) { return $post_id; }
			if(!current_user_can('edit_post', $post_id)) { return $post_id; }
		}
	}
	update_post_meta($post->ID, "zon_status", $_POST["zon_status"]);
}


/*Slider metabox*/
add_action('admin_menu' ,'zontheme_property_slider_metaboxes');
add_action('save_post', 'zontheme_property_slider_metaboxes_save');


function zontheme_property_slider_metaboxes() {
	add_meta_box('zonthemes_property_slider_metabox', 'Slider', 'zontheme_property_slider_metaboxes_view', 'property', 'side', 'high' );
}

function zontheme_property_slider_metaboxes_view() {
	global $post;
	include(INCLUDES_PATH.'/posttypes/property/zonthemes-property-slider-view.php');
}

function zontheme_property_slider_metaboxes_save($post_id) {
	global $post;
	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
	if(isset($_POST['post_type'])) {
		if('page' == $_POST['post_type']) {
			if(!current_user_can('edit_page', $post_id)) { return $post_id; }
			if(!current_user_can('edit_post', $post_id)) { return $post_id; }
		}
	}
	update_post_meta($post->ID, "zon_slider", $_POST["zon_slider"]);
	
}

//config send mail metabox
add_action('admin_menu' ,'zontheme_property_email_metaboxes');
add_action('save_post', 'zontheme_property_email_metaboxes_save');


function zontheme_property_email_metaboxes() {
	add_meta_box('zonthemes_property_email_metabox', 'Email', 'zontheme_property_email_metaboxes_view', 'property', 'advanced', 'high' );
}

function zontheme_property_email_metaboxes_view() {
	global $post;
	include(INCLUDES_PATH.'/posttypes/property/zonthemes-property-email-view.php');
}

function zontheme_property_email_metaboxes_save($post_id) {
	global $post;
	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
	if(isset($_POST['post_type'])) {
		if('page' == $_POST['post_type']) {
			if(!current_user_can('edit_page', $post_id)) { return $post_id; }
			if(!current_user_can('edit_post', $post_id)) { return $post_id; }
		}
	}
	//update_post_meta($post->ID, "zon_slider", $_POST["zon_slider"]);
	
}

?>